﻿CREATE TRIGGER [tri_Note_Last]
ON [dbo].[t_Note_Last]
FOR INSERT
AS

IF @@ROWCOUNT = 0 RETURN

IF NOT EXISTS(SELECT * FROM t_Note N INNER JOIN inserted i ON i.NoteID = N.ID AND N.IsResolved = 0)RETURN

ROLLBACK TRAN
DECLARE @LoanID int SELECT @LoanID = N.LoanID FROM t_Note N INNER JOIN inserted i ON i.NoteID = N.ID AND N.IsResolved = 0
RAISERROR('Inresolved note can not be [Last Note] table. LoanID: %d',16,1,@LoanID)


